package com.sun.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @Author Sun
 * @Date 2020/4/23 16:11
 * @Version 1.0
 */
public class CharacterEncodingFilter implements Filter {
    //启动web服务器时执行
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化过滤器");
    }

    //每进行一次请求都会执行此方法
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");

    //过滤链，让请求继续经过后面的过滤器，如果不写就在此停下
    chain.doFilter(request, response);


    }

    //关闭web服务器时执行
    public void destroy() {
        System.out.println("销毁过滤器");
    }
}
